<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 栅格系统 -->
      <el-row :gutter="20">
        <el-col :span="6">
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 动态渲染表格区域 -->
      <el-table :data="rolesList" border style="width: 100%" stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom',i1==0? 'bdtop':'','vcenter']"
              :gutter="20"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable @close="removeRightById(scope.row, item1.id)">{{item1.authName}}</el-tag>
              </el-col>
              <el-col :span="19">
                <el-row
                  :class="[i2===0? '':'bdtop','vcenter']"
                  v-for="(item2,i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="warning"
                      closable
                      @close="removeRightById(scope.row, item3.id)"
                    >{{item2.authName}}</el-tag>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      @close="removeRightById(scope.row, item.id)"
                      closable
                      type="danger"
                      v-for="item in item2.children"
                      :key="item.id"
                    >{{item.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index"></el-table-column>
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"></el-table-column>
        <el-table-column width="300px" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
            <el-button
              size="mini"
              type="warning"
              icon="el-icon-setting"
              @click="showSetRightDialog(scope.row)"
            >分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分配权限弹出框 -->
    <el-dialog
      title="提示"
      :visible.sync="setRightDialogVisible"
      width="30%"
      @close="setRightDialogClosed"
    >
      <el-tree
        :data="rightslist"
        show-checkbox
        node-key="id"
        :default-checked-keys="defKeys"
        :props="treeProps"
        ref="treeRef"
        default-expand-all
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="allotRights">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      treeProps: {
        label: 'authName',
        hildren: 'children'
      },
      // 获取角色id
      roleId: '',
      // 所有权限的数据
      rightslist: [],
      // 所有角色列表数据
      rolesList: [],
      setRightDialogVisible: false,
      defKeys: []
    }
  },
  created() {
    // 调用数据
    this.getRolesList()
  },
  methods: {
    //  点击为角色分配权限
    async allotRights() {
      const keys = [
        // 全选中的
        ...this.$refs.treeRef.getCheckedKeys(),
        // 半选中的
        ...this.$refs.treeRef.getHalfCheckedKeys()
      ]

      // console.log(keys)

      const idStr = keys.join(',')
      console.log(idStr)

      let { data: res } = await this.$axios.post(
        `roles/${this.roleId}/rights`,
        {
          rids: idStr
        }
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$Message.error(res.meta.msg)
      }
      this.$Message.success('分配权限成功！')

      this.getRolesList()

      this.setRightDialogVisible = false
    },
    // 监听分配权限对话框的关闭事件
    setRightDialogClosed() {
      this.defKeys = []
    },
    // 利用递归循环筛选出符合条件的数据
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }

      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    // 点击分配权限
    async showSetRightDialog(scope) {
      this.roleId = scope.id
      let { data: res } = await this.$axios.get('rights/tree')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$Message.error(res.meta.msg)
      }
      this.rightslist = res.data

      this.getLeafKeys(scope, this.defKeys)
      // 显示弹窗
      this.setRightDialogVisible = true
    },
    // 点击权限的X号删除
    async removeRightById(scope, id) {
      let confirmResult = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => {
        return err
      })
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return
      }
      let { data: res } = await this.$axios.delete(
        `roles/${scope.id}/rights/${id}`
      )
      console.log(res)
      if (res.meta.status !== 200) {
        return this.$Message.error('删除权限失败！')
      }
      this.$Message.success(res.meta.msg)
      scope.children = res.data
    },
    // 请求接口获取roles页面数据
    async getRolesList() {
      let { data: res } = await this.$axios.get('roles')
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$Message.error(res.meta.msg)
      }
      this.rolesList = res.data
    }
  }
}
</script>
<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdtopn {
  border-top: none;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
